import Router from 'koa-router'
import { Models } from '@/common/typings/modle'
import Config from '@/config/Config'
import { Success } from '@/core/HttpException'
import verifyToken from '@/middlewares/verifyToken'
import formidable from 'formidable'
import { upload } from '@/server/qiniu'

const router = new Router({
  prefix: `${Config.API_PREFIX}v1/system/common/upload`,
})

const resourcePath = '/resource/'

/*
 * 上传图片
 */
router.post('/uploadImg', async (ctx: Models.Ctx) => {
  const file = ctx.request.files?.img as unknown as formidable.File
  // console.log(file);
  const res = await upload(file)
  throw new Success({
    data: {
      path: `${resourcePath}${res.key}`,
      name: file.originalFilename,
      mimetype: file.mimetype,
      size: file.size,
    },
  })
})

export default router
